﻿using AIStudio.Wpf.Demo01.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;

namespace AIStudio.Wpf.Demo01.Controls
{
    public class PortControl : ContentControl
    {
        protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
        {
            base.OnMouseLeftButtonDown(e);
            DiagramCanvas canvas = GetDesignerCanvas(this);
            if (canvas != null)
            {
                canvas.SourcePort = this;
            }
        }

        private DiagramCanvas GetDesignerCanvas(DependencyObject element)
        {
            while (element != null && !(element is DiagramCanvas))
                element = VisualTreeHelper.GetParent(element);

            return element as DiagramCanvas;
        }

        public PortBase Info
        {
            get
            {
                if (Content is PortBase port)
                    return port;

                return this.DataContext as PortBase;
            }
        }
    }
}
